#include"FileSystemView.h" 

#include"CommonException.h"
#include"Settings.h"
#include"Song.h"

FileSystemView::FileSystemView(QWidget *parent):QTreeView(parent){
	m_model = new QFileSystemModel(this);
	setDragDropMode(QAbstractItemView::DragOnly);
	setSelectionMode(QAbstractItemView::ExtendedSelection);
	setHeaderHidden(true);//Hides the 'Name' above the dirs
	
	m_model->setRootPath( Settings::instance()->general->musicDir );
	setModel(m_model);
	setRootIndex(m_model->index( Settings::instance()->general->musicDir ));
	for(int i = 1; i < 10; i++)
		setColumnHidden(i,true);
	
	connect(this, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(slotDoubleClicked(const QModelIndex &)));
	connect( Settings::instance(), SIGNAL(generalChanged()), this, SLOT(slotSettingsChanged()) );
}

FileSystemView::~FileSystemView(){
	
}

// #include<stdio.h>
void FileSystemView::slotDoubleClicked(const QModelIndex &index){
	QString filepath = m_model->filePath(index);//Contains full path and filename.
// 	printf("DoubleClicked:\tpath: %s\n", filepath.toAscii().data());
	try{
		QPointer<Song> song = new Song(filepath);
		emit addSong(song);
	}catch(CommonException e){
// 		printf("From FileSystemView:\n");
// 		e.print();
	}
}

void FileSystemView::slotSettingsChanged(){
	m_model->setRootPath( Settings::instance()->general->musicDir );
	setRootIndex(m_model->index( Settings::instance()->general->musicDir ));
}
